* Table A6 - Panel regressions of ex-ante outcomes on productivity shock
* Sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if bal_panel3==1

tostring villageno, replace

*Generate shadow payout variables for control
replace payout_pp = 0 if payout_pp==.

* taluka-based weather station assignment
bys ws_c1 wave : egen payout_pp_all_c1 = max(payout_pp)
* geography-based weather station assignment
bys ws_c2 wave : egen payout_pp_all_c2 = max(payout_pp)

* only control farmers
keep if treat_year==0

* total payout by weather station assignment
replace payout_pp_all_c1=payout_pp_all_c1*1000
replace payout_pp_all_c2=payout_pp_all_c2*1000

* creating table
local table = 1
local rastring "replace"
local outcomevars "kval_w1_real cost_k_w1_real b110rk_w1_real b111rk_w1_real b112pk_w1_real total_mandays_k_w1 total_hired_mandays_k_w1 total_fam_mandays_k_w1 profit_k_w1_real  fraction_hy fraction_cashcrop plotsize_ha"
foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV with Individual FE*/
	xtreg `ovar' payout_pp_all_c1 year2*  firstt_2007 lost_w3 resurveyed , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	xtreg `ovar' payout_pp_all_c1 firstt_2007 lost_w3 resurveyed, fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	qui estout cT`table'C* using "../output/a6_a.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (payout_pp_all_c1) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

local rastring "replace"

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	xtreg `ovar' payout_pp_all_c2 year2*  firstt_2007 lost_w3 resurveyed, fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	xtreg `ovar' payout_pp_all_c2 year2*  firstt_2007 lost_w3 resurveyed, fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	qui estout cT`table'C* using "../output/a6_b.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (payout_pp_all_c2) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

local rastring "replace"


